package com.eqdd.yiqidian.ui.discover.gonggao;

import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;

import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.RefreshRecyclerViewFragmentCustom;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.BaseFragment;
import com.eqdd.yiqidian.bean.rebuild.NewsResultBean;
import com.eqdd.yiqidian.bean.rebuild.NoticeListResultBean;
import com.eqdd.yiqidian.bean.rebuild.ListResultBean;
import com.eqdd.yiqidian.bean.rebuild.RedPaperBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditHintBean;
import com.eqdd.yiqidian.helper.mvchelper.ModelRx2DataSource;
import com.eqdd.yiqidian.helper.mvchelper.Rx2DataSource;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.http.JsonConverter;
import com.eqdd.yiqidian.ui.mine.pizhun.NewsInfoActivity;
import com.eqdd.yiqidian.utils.GreenDaoUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.lzy.okrx2.adapter.FlowableBody;
import com.shizhefei.mvc.MVCSwipeRefreshHelper;

import org.reactivestreams.Publisher;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Flowable;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Function;

/**
 * Created by lvzhihao on 17-6-21.
 */

public class TonggaoFragment extends BaseFragment {
    private RefreshRecyclerViewFragmentCustom dataBinding;
    private SlimAdapterEx<NoticeListResultBean> slimAdapterEx;
    private String status;
    private ModelRx2DataSource<NoticeListResultBean> dataSource;
    private MVCSwipeRefreshHelper<List<NoticeListResultBean>> mvcHelper;
    private int pageNum = 0;

    @Override
    protected void setView() {


    }

    @Override
    protected void initData() {

        if (slimAdapterEx == null) {
            dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
            slimAdapterEx = SlimAdapterEx.create().register(R.layout.list_item_13, new SlimInjector<NoticeListResultBean>() {
                @Override
                public void onInject(NoticeListResultBean data, IViewInjector injector) {
                    injector.text(R.id.tv_left_center_content, data.getName())
                            .text(R.id.tv_upper_right_content, data.getTheme())
                            .text(R.id.tv_down_right_content, data.getCreateTime());
                }
            })
                    .attachTo(dataBinding.recyclerView);
            ItemClickSupport.addTo(dataBinding.recyclerView)
                    .setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {
                        @Override
                        public void onItemClicked(RecyclerView recyclerView, int position, View v) {
//                            Bundle bundle = new Bundle();
//                            bundle.putString("newsId", slimAdapterEx.getData().get(position).getId() + "");
//                            startActivity(NewsInfoActivity.class, bundle);
                            NoticeListResultBean itemBean = slimAdapterEx.getData().get(position);

                            OkGo.<HttpResult<NewsResultBean>>post(HttpConfig.BASE_URL + HttpConfig.NEWS_INFO)
                                    .params("newsId",itemBean.getId() )
                                    .execute(new DialogCallBack<HttpResult<NewsResultBean>>((BaseActivity) getActivity()) {
                                        @Override
                                        public void onSuccess(Response<HttpResult<NewsResultBean>> response) {
                                            HttpResult<NewsResultBean> httpResult = response.body();
                                            ToastUtil.showShort(httpResult.getMsg());
                                            if (httpResult.getStatus() == 200) {
                                                NewsResultBean item = httpResult.getItems();
                                                RedPaperBean redPaperBean = new RedPaperBean()
                                                        .setTitle("通告")
                                                        .setCode(item.getNewsCode())
                                                        .setPublisher(item.getCreateName())
                                                        .setContent(item.getNewsContent())
                                                        .setDate(item.getCreateTime())
                                                        .setYear(item.getCreateTime().substring(0, 4))
                                                        .setPaperTitle(item.getNewsName())
                                                        .setTheme(item.getNewsTheme());
                                                Bundle bundle = new Bundle();
                                                bundle.putSerializable("bean", redPaperBean);
                                                ((BaseActivity) getActivity()).startActivity(RedPaperActivity.class, bundle);
                                            }
                                        }
                                    });
                        }
                    });
            dataSource = new ModelRx2DataSource<>(new ModelRx2DataSource.OnLoadSource() {
                @Override
                public Flowable<List> loadSource(int page, Rx2DataSource.DoneActionRegister register) {
                    return OkGo.<HttpResult<ListResultBean<NoticeListResultBean>>>post(HttpConfig.BASE_URL + HttpConfig.ALL_MY_NEWS)
                            .params("page", pageNum)
                            .params("companyId", GreenDaoUtil.getUser().getCompanyId())
                            .params("departId", GreenDaoUtil.getUser().getDepartId())
                            .converter(new JsonConverter<HttpResult<ListResultBean<NoticeListResultBean>>>() {
                                @Override
                                public void test() {
                                    super.test();
                                }
                            }).adapt(new FlowableBody<HttpResult<ListResultBean<NoticeListResultBean>>>())
                            .flatMap(new Function<HttpResult<ListResultBean<NoticeListResultBean>>, Publisher<List<NoticeListResultBean>>>() {
                                @Override
                                public Publisher<List<NoticeListResultBean>> apply(@NonNull HttpResult<ListResultBean<NoticeListResultBean>> noticeWraperResultBeanHttpResult) throws Exception {
                                    pageNum = page == 1 ? 0 : noticeWraperResultBeanHttpResult.getItems().getPage();
                                    return Flowable.just(noticeWraperResultBeanHttpResult.getItems().getList() == null ? new ArrayList<NoticeListResultBean>() : noticeWraperResultBeanHttpResult.getItems().getList());
                                }
                            });

                }
            }, 10);
            slimAdapterEx.updateData(new ArrayList<NoticeListResultBean>());
            mvcHelper = new MVCSwipeRefreshHelper<>(dataBinding.swipeRefresh);
            mvcHelper.setDataSource(dataSource);
            mvcHelper.setAdapter(slimAdapterEx);
            mvcHelper.refresh();
        }
    }

    @Override
    public ViewDataBinding initBinding(LayoutInflater inflater) {
        return dataBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_refresh_recycler_view, null, false);
    }

    @Override
    public void onClick(View v) {

    }
}
